package com.zl.tcbnewcar.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.foamtrace.photopicker.ImageCaptureManager;
import com.foamtrace.photopicker.SelectModel;
import com.foamtrace.photopicker.intent.PhotoPickerIntent;
import com.google.gson.Gson;
import com.zl.tcbnewcar.R;
import com.zl.tcbnewcar.base.BaseActivity;
import com.zl.tcbnewcar.model.CarDealerEntity;
import com.zl.tcbnewcar.model.CarUserEntity;
import com.zl.tcbnewcar.ui.dialog.PhotoDialog;
import com.zl.tcbnewcar.utils.API;
import com.zl.tcbnewcar.utils.HttpUtils;
import com.zl.tcbnewcar.utils.ImageLoader;
import com.zl.tcbnewcar.utils.PhotoBitmapUtils;
import com.zl.tcbnewcar.utils.SpUtlis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;

import static com.foamtrace.photopicker.PhotoPickerActivity.EXTRA_RESULT;
import static com.zl.tcbnewcar.ui.dialog.PhotoDialog.PICK_FROM_CAMERA;

public class RealCarDealerActivity extends BaseActivity {

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_share)
    ImageView ivTitleShare;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.iv_person_front)
    ImageView ivPersonFront;
    @BindView(R.id.iv_person_back)
    ImageView ivPersonBack;
    @BindView(R.id.iv_license)
    ImageView ivLicense;
    @BindView(R.id.tv_commit_real)
    TextView tvCommitReal;
    private String frontPath, backPath, licensePath;
    private CarDealerEntity carDealerEntity;
    private PhotoDialog photoFrontDialog;
    private PhotoDialog photoBackDialog;
    private PhotoDialog photolicenseDialog;
    //身份证相册选择（反面）
    private final int BACKALBUM = 980;
    //身份证相册选择（正面）
    private final int FRONTALBUM = 986;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_car_dealer);
        ButterKnife.bind(this);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        photoBackDialog.setOnChoosePhotoListener(new PhotoDialog.OnChoosePhotoListener() {
            @Override
            public void openAlbum() {
                PhotoPickerIntent intent = new PhotoPickerIntent(mActivity);
                intent.setSelectModel(SelectModel.SINGLE);
                intent.setShowCarema(false); // 是否显示拍照， 默认false
                mActivity.startActivityForResult(intent, BACKALBUM);
            }

            @Override
            public void openCamera() {
                Intent intent1 = new Intent(mActivity, CameraActivity.class);
                intent1.putExtra("type", 1);
                startActivityForResult(intent1, 11);
            }
        });
        photoFrontDialog.setOnChoosePhotoListener(new PhotoDialog.OnChoosePhotoListener() {
            @Override
            public void openAlbum() {
                PhotoPickerIntent intent = new PhotoPickerIntent(mActivity);
                intent.setSelectModel(SelectModel.SINGLE);
                intent.setShowCarema(false); // 是否显示拍照， 默认false
                mActivity.startActivityForResult(intent, FRONTALBUM);
            }

            @Override
            public void openCamera() {
                Intent intent = new Intent(mActivity, CameraActivity.class);
                intent.putExtra("type", 0);
                startActivityForResult(intent, 10);
            }
        });
        photolicenseDialog.setOnChoosePhotoListener(new PhotoDialog.OnChoosePhotoListener() {
            @Override
            public void openAlbum() {
                openSingleAlbum(12);
            }

            @Override
            public void openCamera() {
                photolicenseDialog.openCamera();
            }
        });
    }

    private void initData() {
        Map<String, String> params = new HashMap<>();
        params.put("did", SpUtlis.getUserData(mActivity).getCarDealerId() + "");
        params.put("isSee", true + "");
        HttpUtils.getInstance().Post(mActivity, params, API.getCarDealerById, new HttpUtils.OnOkHttpCallback() {
            @Override
            public void onSuccess(String body) {
                carDealerEntity = new Gson().fromJson(body, CarDealerEntity.class);
                updateUi();
            }

            @Override
            public void onError(Request error, Exception e) {

            }
        });
    }

    /**
     * 更新UI
     */
    private void updateUi() {
        if (TextUtils.isEmpty(carDealerEntity.getDealerBusinessLicense())) {
            showToast("您还未进行车行认证");
            return;
        }

        ImageLoader.loadImageUrl(mActivity, carDealerEntity.getDealerBusinessLicense(), ivLicense);
        ImageLoader.loadImageUrl(mActivity, carDealerEntity.getDealerLegalIdP(), ivPersonFront);
        ImageLoader.loadImageUrl(mActivity, carDealerEntity.getDealerLegalIdN(), ivPersonBack);
        ivLicense.setEnabled(false);
        ivPersonFront.setEnabled(false);
        ivPersonBack.setEnabled(false);
        tvCommitReal.setEnabled(false);
        tvCommitReal.setVisibility(View.GONE);
    }

    private void initView() {
        tvTitleName.setText("认证车行");
        photoFrontDialog = new PhotoDialog(mActivity);
        photoBackDialog = new PhotoDialog(mActivity);
        photolicenseDialog = new PhotoDialog(mActivity);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 10:
                    frontPath = data.getStringExtra("path");
                    ImageLoader.loadImageFile(mActivity, frontPath, ivPersonFront);
                    break;
                case 11:
                    backPath = data.getStringExtra("path");
                    ImageLoader.loadImageFile(mActivity, backPath, ivPersonBack);
                    break;
                //营业执照
                case 12:
                    ArrayList<String> list = data.getStringArrayListExtra(EXTRA_RESULT);
                    licensePath = list.get(0);
                    ImageLoader.loadImageFile(mActivity, licensePath, ivLicense);
                    break;
                //营业执照
                case ImageCaptureManager.REQUEST_TAKE_PHOTO:
                    ArrayList<String> list1 = data.getStringArrayListExtra(EXTRA_RESULT);
                    licensePath = list1.get(0);
                    ImageLoader.loadImageFile(mActivity, licensePath, ivLicense);
                    break;
                //营业执照
                case PICK_FROM_CAMERA:
                    String imagePath = photolicenseDialog.getImagePath();
                    //修改小米手机拍照后图片旋转的问题
                    licensePath = PhotoBitmapUtils.amendRotatePhoto(imagePath, mActivity);
                    ImageLoader.loadImageFile(mActivity, licensePath, ivLicense);
                    break;
                case FRONTALBUM:
                    ArrayList<String> frontList = data.getStringArrayListExtra(EXTRA_RESULT);
                    frontPath = frontList.get(0);
                    ImageLoader.loadImageFile(mActivity, frontPath, ivPersonFront);
                    break;
                case BACKALBUM:
                    ArrayList<String> backList = data.getStringArrayListExtra(EXTRA_RESULT);
                    backPath = backList.get(0);
                    ImageLoader.loadImageFile(mActivity, backPath, ivPersonBack);
                    break;
            }
        }
    }

    @OnClick({R.id.iv_title_back, R.id.iv_person_front, R.id.iv_person_back, R.id.tv_commit_real, R.id.iv_license})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_commit_real:
                commit();
                break;
            case R.id.iv_person_front:
                photoFrontDialog.showDialog(view);
                break;
            case R.id.iv_person_back:
                photoBackDialog.showDialog(view);
                break;
            case R.id.iv_license:
                photolicenseDialog.showDialog(view);
                break;
        }
    }

    private void commit() {

        if (TextUtils.isEmpty(frontPath) || TextUtils.isEmpty(backPath)) {
            showToast("请上传身份证正反面");
            return;
        }

        if (TextUtils.isEmpty(licensePath)) {
            showToast("请上传营业执照");
            return;
        }

        CarUserEntity userData = SpUtlis.getUserData(mActivity);
        Map<String, String> params = new HashMap<>();
        params.put("uid", userData.getId() + "");
        params.put("did", userData.getCarDealerId() + "");
        List<String> list = new ArrayList<>();
        list.add(frontPath);
        list.add(backPath);
        list.add(licensePath);
        pDialog.setTitleText("认证中...");
        pDialog.show();
        HttpUtils.getInstance().upLoadFile(mActivity, params, API.replenishCarDealerInfo, list, new HttpUtils.OnOkHttpCallback() {
            @Override
            public void onSuccess(String body) {
                pDialog.hide();
                Log.e("body", body);
                CarUserEntity user = SpUtlis.getUserData(mActivity);
                user.setUserDealerApply(1);
                SpUtlis.setUserData(mActivity,user);
                showToast("车行实名认证成功");
                finish();
            }

            @Override
            public void onError(Request error, Exception e) {
                pDialog.hide();
                showToast(e.getMessage());
            }
        });
    }


}
